//
//  TableViewController.m
//  demo3
//
//  Created by 张仕儒 on 2021/7/9.
//

#import "TableView.h"
#import "TableHeader.h"

@interface TableView()
@property (weak) IBOutlet NSTableView *myTableView;
@property (weak) IBOutlet NSTableHeaderView *tableHeader;

@end

@implementation TableView

+ (instancetype)initWithNibName:(NSString *)nibName {
    if (nibName == nil) {
        nibName = @"TableView";
    }
    NSNib *nib = [[NSNib alloc] initWithNibNamed:nibName bundle:nil];
    NSArray *objects = nil;
    BOOL isInitial = [nib instantiateWithOwner:self topLevelObjects:&objects];
    if (isInitial) {
        for (id obj in objects) {
            if([obj isKindOfClass:[TableView class]]) {
                TableView *table = (TableView *)obj;
                table.myTableView.delegate = table;
                table.myTableView.dataSource = table;
                return table;
            }
        }
    }
    return nil;
}

- (void)initView {
    TableHeader *header = [TableHeader initWithNibName:@"TableHeader"];
    [self.myTableView setHeaderView:header];
//    self.statusCell.tableView = [TableStatusCell init];
    
}

- (void)awakeFromNib{
    [super awakeFromNib];
    NSLog(@"");
}

- (NSMutableArray *)software {
    if(!_software) {
        _software = [[NSMutableArray alloc]initWithCapacity:count];
        for(int i=0;i<count;i++) {
            [_software addObject:@"nsfnv.app"];
        }
    }
    return _software;
}

- (NSMutableArray *)category {
    if(!_category) {
        _category = [[NSMutableArray alloc]initWithCapacity:count];
        for(int i=0;i<count;i++) {
            if(i%2==0) {
                [_category addObject:@"软件"];
            }else {
                [_category addObject:@"文件夹"];
            }
        }
    }
    return _category;
}

- (NSMutableArray *)status {
    if(!_status) {
        _status = [[NSMutableArray alloc]initWithCapacity:count];
        for(int i=0;i<count;i++) {
            [_status addObject: [NSNumber numberWithInt:arc4random()%4]];
        }
    }
    return _status;
}

- (NSMutableArray *)processPerson {
    if(!_processPerson) {
        _processPerson = [[NSMutableArray alloc]initWithCapacity:count];
        for(int i=0;i<count;i++) {
            [_processPerson addObject:@"本机用户"];
        }
    }
    return _processPerson;
}

- (NSMutableArray *)developer {
    if(!_developer) {
        _developer = [[NSMutableArray alloc]initWithCapacity:count];
        for(int i=0;i<count;i++) {
            [_developer addObject:@"Apple Inc"];
        }
    }
    return _developer;
}

- (NSMutableArray *)accessIn {
    if(!_accessIn) {
        _accessIn = [[NSMutableArray alloc]initWithCapacity:count];
        for(int i=0;i<count;i++) {
            if(i%2==0) {
                [_accessIn addObject:@"YES"];
//                [_accessIn addObject:[NSNumber numberWithBool:YES]];
            }else {
                [_accessIn addObject:@"NO"];
//                [_accessIn addObject:[NSNumber numberWithBool:NO]];
            }
        }
    }
    return _accessIn;
}

- (NSMutableArray *)accessOut {
    if(!_accessOut) {
        _accessOut = [[NSMutableArray alloc]initWithCapacity:count];
        for(int i=0;i<count;i++) {
            if(i%2==0) {
                [_accessOut addObject:@"YES"];
            }else {
                [_accessOut addObject:@"NO"];
            }
        }
    }
    return _accessOut;
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
    return count;
}


-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    NSString *identifier = tableColumn.identifier;
    NSView *view = [tableView makeViewWithIdentifier:identifier owner:self];
    NSArray *subViews = [view subviews];
    if ([identifier isEqualToString:@"software"]) {
        NSTextField *textField = subViews[0];
        textField.stringValue = [self.software objectAtIndex:row];
    } else if ([identifier isEqualToString:@"category"]){
        NSTextField *textField = subViews[0];
        textField.stringValue = [self.category objectAtIndex:row];
    } else if ([identifier isEqualToString:@"status"]) {
        NSImageView *image = subViews[0];
        NSTextField *textField = subViews[1];
        switch ([[self.status objectAtIndex:row] integerValue]) {
            case 0:
                [image setImage:[NSImage imageNamed:@"NSStatusUnavailable"]];
                textField.stringValue = @"禁止软件";
                break;
            case 1:
                [image setImage:[NSImage imageNamed:@"NSStatusPartiallyAvailable"]];
                textField.stringValue = @"无发行商软件";
                break;
            case 2:
                [image setImage:[NSImage imageNamed:@"NSStatusPartiallyAvailable"]];
                textField.stringValue = @"普通软件";
                break;
            case 3:
                [image setImage:[NSImage imageNamed:@"NSStatusAvailable"]];
                textField.stringValue = @"公司软件";
                break;
            default:
                break;
        }
    } else if ([identifier isEqualToString:@"processPerson"]){
        NSTextField *textField = subViews[0];
        textField.stringValue = [self.processPerson objectAtIndex:row];
    } else if ([identifier isEqualToString:@"developer"]){
        NSTextField *textField = subViews[0];
        textField.stringValue = [self.developer objectAtIndex:row];
    } else if ([identifier isEqualToString:@"accessIn"]) {
        NSSwitch *mySwitch = subViews[0];
        if([[self.accessIn objectAtIndex:row] isEqual: @"YES"]) {
            [mySwitch setState:NSControlStateValueOn];
        }else {
            [mySwitch setState:NSControlStateValueOff];
        }
    } else if ([identifier isEqualToString:@"accessOut"]) {
        NSSwitch *mySwitch = subViews[0];
        if([[self.accessOut objectAtIndex:row] isEqual: @"YES"]) {
            [mySwitch setState:NSControlStateValueOn];
        }else {
            [mySwitch setState:NSControlStateValueOff];
        }
    }

    return view;
}
@end
